package com.example.user3.demo;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;

import androidx.annotation.Nullable;

public class ArcLayout extends View {
    public int color= Color.parseColor("#50000000") ;
    private float mProgress;      // 圆环进度(0-100)
    Paint mProgPaint;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    CallBack callBack;
    public interface CallBack {
        void onEnd();
    };
    public ArcLayout(Context context) {
        super(context);
    }

    public ArcLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    Paint paint;
    PorterDuffXfermode porterDuffXfermode;
    private void initView() {
        mProgPaint= new Paint();
        mProgPaint.setColor(color);
        mProgPaint.setStyle(Paint.Style.FILL );    // 只描边，不填充
        mProgPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        mProgPaint.setAntiAlias(true);              // 设置抗锯齿
        mProgPaint.setDither(true);
         paint =new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
//        setBackgroundColor(color);
    }

    public ArcLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public ArcLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    Bitmap bitmap,mOut;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hb);
        Log.i("zzy",bitmap.toString()+"--"+bitmap.getWidth());
        mOut = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Log.i("zzy",mOut.toString()+"--"+mOut.getWidth());
//        RectF rectF = new RectF(-(getWidth()/4), (-getHeight()/4), getWidth()/4*5, getHeight()/4*5);
//        canvas.drawArc(rectF, mProgress-90, 360-mProgress, true, mProgPaint);
//        canvas.drawRoundRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), 80, 80, mProgPaint);
//        mProgPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN));
        canvas.drawBitmap(mOut,0,0,paint);
//        canvas.drawRoundRect(0, 0, mBitmap.getWidth(), mBitmap.getHeight(), 80, 80, mPaint);
//        canvas.drawRoundRect(rectF,10,10,mProgPaint);
//        init( canvas);
    }

    /**
     * 设置当前进度，并展示进度动画。如果动画时间小于等于0，则不展示动画
     *
     * @param progress 当前进度（0-100）
     * @param animTime 动画时间（毫秒）
     */
    public void setProgress(float progress, long animTime) {
        if (animTime <= 0) setProgress(progress);
        else {
            ValueAnimator animator = ValueAnimator.ofFloat(mProgress, progress);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mProgress = (float) animation.getAnimatedValue();
                    Log.i("zzy",mProgress+"--");
                    invalidate();
                }
            });
            animator.setInterpolator(new LinearInterpolator());
            animator.setDuration(animTime);
            animator.start();
            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    callBack.onEnd();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        }
    }
    /**
     * 设置当前进度
     *
     * @param progress 当前进度（0-360）
     */
    public void setProgress(float progress) {
        this.mProgress = progress;
        invalidate();
    }
}
